package com.qsbc.pygram.controller;

import com.qsbc.pygram.common.Constants;
import com.qsbc.pygram.common.Result;
import com.qsbc.pygram.entity.Item;
import com.qsbc.pygram.entity.PaperManage;
import com.qsbc.pygram.service.JudgeQuestionService;
import com.qsbc.pygram.service.MultiQuestionService;
import com.qsbc.pygram.service.PaperManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/pygram/item")
public class ItemController {

    @Autowired
    MultiQuestionService multiQuestionService;

    @Autowired
    JudgeQuestionService judgeQuestionService;

    @Autowired
    PaperManageService paperManageService;




    @PostMapping("/item")
    public Result ItemController(@RequestBody Item item) {
        // 选择题
        Integer changeNumber = item.getChangeNumber();
        // 判断题
        Integer judgeNumber = item.getJudgeNumber();
        //出卷id
        Integer paperId = item.getPaperId();

         //选择题数据库获取
        List<Integer> changeNumbers = multiQuestionService.findBySubject(item.getSubject(), changeNumber);
        if(changeNumbers==null){
            return Result.error(Constants.CODE_500,"选择题数据库获取失败");
        }
        for (Integer number : changeNumbers) {
            PaperManage paperManage = new PaperManage();
            int index = paperManageService.add(paperManage);
            if(index==0)
                return Result.error(Constants.CODE_500,"选择题数据库获取失败");
        }



        // 判断题
        List<Integer> judges = judgeQuestionService.findBySubject(item.getSubject(), judgeNumber);
        if(judges==null)
            return Result.error(Constants.CODE_500,"选择题数据库获取失败");
        for (Integer judge : judges) {
            PaperManage paperManage = new PaperManage();
            int index = paperManageService.add(paperManage);
            if(index==0)
                return Result.error(Constants.CODE_500,"选择题数据库获取失败");
        }


        return Result.success(null);
    }
}
